package com.trust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trust.agri.util.BeanUtil;
import com.trust.entity.Dictionary;
import com.trust.entity.VO.DictionaryVO;
import com.trust.mapper.DictionaryMapper;
import com.trust.service.DictionaryService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Override
    public Integer save(Integer parentId, String name) {
        Dictionary dictionary = new Dictionary();
        dictionary.setEnabled(1);
        dictionary.setParentId(parentId);
        dictionary.setName(name);
        this.save(dictionary);
        return dictionary.getId();
    }


    @Override
    public String getNameById(Integer id) {
        Dictionary dictionary = this.getById(id);
        if (null == dictionary){
            return "";
        }
        return dictionary.getName();
    }

    @Override
    public List<DictionaryVO> dropDownList(Integer parentId) {
        LambdaQueryWrapper<Dictionary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dictionary::getParentId, parentId);
        wrapper.eq(Dictionary::getEnabled, 1);
        wrapper.orderByAsc(Dictionary::getId);
        List<Dictionary> list = this.list(wrapper);
        return BeanUtil.sourceList2targetList(list, DictionaryVO.class);
    }

}
